<?php
/**
 * Created by PhpStorm.
 * User: bolle
 * Date: 21.12.15
 * Time: 10:04
 */

namespace tbollmeier\alteraparser\syntaxgraph;


class Vertex extends AbstractVertex implements Dockable
{

    private $successors;
    private $category;

    public function __construct($category=VertexCategory::NORMAL)
    {
        $this->successors = [];
        $this->category = $category;
    }

    public function numSuccessors()
    {
        return count($this->successors);
    }

    public function nthSuccessor($idx)
    {
        return $idx >= 0 && $idx < count($this->successors) ?
            $this->successors[$idx] : null;
    }

    protected function onCopy($original)
    {
        $this->category = $original->category;
        // TODO: check if successors have to be cloned
    }

    public function connect(Dockable $dockable)
    {
        array_push($this->successors, $dockable->getDockVertex());
        return $dockable;
    }

    public function getDockVertex()
    {
        return $this;
    }

    public function getCategory()
    {
        return $this->category;
    }

    public function isNormal()
    {
        return $this->category === VertexCategory::NORMAL;
    }

    public function isGroupStart()
    {
        return $this->category === VertexCategory::GROUP_START;
    }

    public function isGroupEnd()
    {
        return $this->category === VertexCategory::GROUP_END;
    }

    public function isMatcher()
    {
        return $this->category === VertexCategory::MATCHER;
    }

    public function isEnd()
    {
        return $this->category === VertexCategory::END;
    }

}